package com.cloud.school.bus.teacherhelper.modules;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.support.jhf.debug.DebugLog;
import com.android.support.jhf.network.NetworkClient;
import com.cloud.school.bus.teacherhelper.R;
import com.cloud.school.bus.teacherhelper.base.baidu.baidupush.BaiduPushUtils;
import com.cloud.school.bus.teacherhelper.base.dialog.CustomAlertDialog;
import com.cloud.school.bus.teacherhelper.base.slidingfragment.BaseBehindFragment;
import com.cloud.school.bus.teacherhelper.base.utils.AppUserId;
import com.cloud.school.bus.teacherhelper.entitys.ClassInfo;
import com.cloud.school.bus.teacherhelper.entitys.LoginSetting;
import com.cloud.school.bus.teacherhelper.entitys.Student;
import com.cloud.school.bus.teacherhelper.entitys.Teacher;
import com.cloud.school.bus.teacherhelper.modules.home.HomeFragment;
import com.cloud.school.bus.teacherhelper.modules.information.InformationFragment;
import com.cloud.school.bus.teacherhelper.modules.login.LoginActivity;
import com.cloud.school.bus.teacherhelper.modules.notice.NoticeFragment;
import com.cloud.school.bus.teacherhelper.modules.settings.SettingsFragment;
import com.cloud.school.bus.teacherhelper.modules.stuclass.StudentListFragment;
import com.cloud.school.bus.teacherhelper.modules.uploadlist.UploadListFragment;

/**
 * 主界面菜单
 * 
 * @author lenovo
 * 
 */
public class HomeMenuFragment extends BaseBehindFragment {

	public static final String UPDATE_INFOR_COUNT = "update_infor_count";

	/** 先设置一个静态变量，以后优化 */
	public static TextView mInforCountTextView;

	private ViewGroup mInformationLayout;

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		View view = inflater.inflate(R.layout.fragment_home_menu, null);
		setViewData(view);
		return view;
	}

	protected void setViewData(View view) {

		mInforCountTextView = (TextView) view
				.findViewById(R.id.inforCountTextView);
		String inforCountString = "";
		int inforCountInt = BaiduPushUtils.getNewInformationCount(mApplication);
		if (inforCountInt > 0) {
			inforCountString = inforCountInt + "";
		}
		mInforCountTextView.setText(inforCountString);

		mInformationLayout = (ViewGroup) view
				.findViewById(R.id.informationLayout);
		final ViewGroup stuclassLayout = (ViewGroup) view
				.findViewById(R.id.stuclassLayout);
		final ViewGroup settingsLayout = (ViewGroup) view
				.findViewById(R.id.settingsLayout);
		final ViewGroup noticeLayout = (ViewGroup) view
				.findViewById(R.id.noticeLayout);
		final ViewGroup uploadListLayout = (ViewGroup) view
				.findViewById(R.id.uploadListLayout);

		mInformationLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				BaiduPushUtils.setNewInformationCount(mApplication, 0);
				mInforCountTextView.setText("");
				mInformationLayout.setSelected(true);
				stuclassLayout.setSelected(false);
				settingsLayout.setSelected(false);
				noticeLayout.setSelected(false);
				uploadListLayout.setSelected(false);
				switchFragment(new InformationFragment());
			}
		});

		stuclassLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mInformationLayout.setSelected(false);
				stuclassLayout.setSelected(true);
				settingsLayout.setSelected(false);
				noticeLayout.setSelected(false);
				uploadListLayout.setSelected(false);
				switchFragment(new StudentListFragment());
			}
		});

		settingsLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mInformationLayout.setSelected(false);
				stuclassLayout.setSelected(false);
				settingsLayout.setSelected(true);
				noticeLayout.setSelected(false);
				uploadListLayout.setSelected(false);
				switchFragment(new SettingsFragment());
			}
		});

		noticeLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mInformationLayout.setSelected(false);
				stuclassLayout.setSelected(false);
				settingsLayout.setSelected(false);
				noticeLayout.setSelected(true);
				uploadListLayout.setSelected(false);
				switchFragment(new NoticeFragment());
			}
		});

		uploadListLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				mInformationLayout.setSelected(false);
				stuclassLayout.setSelected(false);
				settingsLayout.setSelected(false);
				noticeLayout.setSelected(false);
				uploadListLayout.setSelected(true);
				switchFragment(new UploadListFragment());
			}
		});
		Button homeButton = (Button) view
				.findViewById(R.id.homeButton);
		homeButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
				mInformationLayout.setSelected(false);
				stuclassLayout.setSelected(false);
				settingsLayout.setSelected(false);
				noticeLayout.setSelected(false);
				uploadListLayout.setSelected(false);
				
				switchFragment(new HomeFragment());
			}
		});

		ViewGroup logoutLayout = (ViewGroup) view
				.findViewById(R.id.logoutLayout);
		logoutLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				logoutDialog();
			}
		});
	}

	private void logoutDialog() {
		CustomAlertDialog customAlertDialog = new CustomAlertDialog(
				mParentContext, R.style.CustomAlertDialog);
		customAlertDialog.setTitleMessage(getString(R.string.tips), Color.BLACK);
		customAlertDialog.setMessage(getString(R.string.confirm_to_logout));
		customAlertDialog.setLeftButton(getString(R.string.logout), new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				//清除内存的缓存
				/** 教师信息 */
				mApplication.mTeacher = null;
				/** 教师账号中的学生列表 */
				mApplication.mStudentList = null;
				/** 班级信息 */
				mApplication.mClassInfo = null;
				/**登陆设置*/
				mApplication.mLoginSetting = null;
				
				NetworkClient.getNetworkClient().setLoginToken("");
				// AppUserId.setUserName(mParentContext, "");
				AppUserId.setPassword(mParentContext, "");
				Intent intent = new Intent(mParentContext, LoginActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent);
				((Activity) mParentContext).finish();
			}
		});
		customAlertDialog.setRightButton(getString(R.string.cancel), new OnClickListener() {

			@Override
			public void onClick(View arg0) {

			}
		});
		customAlertDialog.show();
	}

	// the meat of switching the above fragment
	private void switchFragment(Fragment fragment) {
		if (getActivity() == null)
			return;

		if (getActivity() instanceof SlidingActivity) {
			SlidingActivity fca = (SlidingActivity) getActivity();
			fca.switchContent(fragment);
		}
	}

	private BroadcastReceiver mUpdateUiBroadcastReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			DebugLog.logI("mUpdateUiBroadcastReceiver HomeMenuFragment");
			if (UPDATE_INFOR_COUNT.equals(intent.getAction())) {
				if (mInformationLayout.isSelected()) {

				} else {
					HomeMenuFragment.mInforCountTextView.setText(BaiduPushUtils
							.getNewInformationCount(context) + "");
				}

			}
		}
	};

	@Override
	public void onResume() {
		super.onResume();
		// 注册广播
		IntentFilter intentFilter = new IntentFilter(UPDATE_INFOR_COUNT);
		mParentContext.registerReceiver(mUpdateUiBroadcastReceiver,
				intentFilter);
	}

	@Override
	public void onPause() {
		super.onPause();
		mParentContext.unregisterReceiver(mUpdateUiBroadcastReceiver);
	}

}
